package com.rentalcar.rental_car.controller;

import com.rentalcar.rental_car.entity.Car;
import com.rentalcar.rental_car.entity.Result;
import com.rentalcar.rental_car.service.CarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@Slf4j
@RequestMapping("/car")
public class CarController {
    @Autowired
    CarService carService;
    @PostMapping (value = "/carList")
    public Result getList(){
        try {
            log.info("查询全部车辆");
            return Result.success(carService.getCarAll());
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("错误");
        }
    }
    @PostMapping("/selectType")
    public Result selectType(@RequestBody Car car){
        try {
            log.info("查找" + car.getType() + "类型的车");
            return Result.success(carService.selectByType(car.getType()));
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail();
        }
    }
    @PostMapping("/selectAddress")
    public Result selectAddress(@RequestBody Car car){
        try {
        log.info("查找"+car.getAddress()+"的车辆");
        return Result.success(carService.selectByAddress(car.getAddress()));
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail();
        }
    }
    @PostMapping("/selectColor")
    public Result selectColor(@RequestBody Car car){
        try {
            log.info("查找"+car.getColor()+"的车辆");
            return Result.success(carService.selectByColor(car.getColor()));
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail();
        }
    }
    @PostMapping("/addCar")
    public Result addCar(@RequestBody Car car){
        try {
            log.info("增添车辆,车牌号为：" + car.getLicense());
            carService.add(car);
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail();
        }
    }
    @PostMapping("/updateCar")
    public Result update(@RequestBody Car car){
        try {
            log.info("修改车辆信息，车牌号为：" + car.getLicense());
            carService.update(car);
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail();
        }
    }
    @PostMapping("/deleteCar")
    public Result delete(@RequestBody Car car){
        try {
            log.info("删除一辆车,车编号为：" + car.getLicense());
            carService.delete(car);
            return Result.success();
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail();
        }
    }
}
